package com.beerandjesus.photogallery.init;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.apache.log4j.Logger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

/**
 * Properties that are loaded upon the contextInitialized() event, from an
 * application properties file on the classpath, and a local properties
 * file on the filesystem.
 * <p/>
 * The application properties file must define this property:
 * <p/>
 * com.beerandjesus.photogallery.basedir
 * <p/>
 * such that the local properties file can be found at the following
 * location on the filesystem:
 * <p/>
 * basedir + servletContextPath
 */
public final class PhotoGalleryProperties implements ServletContextListener {

    private static final Properties PROPS = new Properties();

    private static final Logger LOG = Logger.getLogger(PhotoGalleryProperties.class);

    private String appPropertiesFile = "photogallery.properties";
    private String contextPropertiesFile = "photogallery.properties";
    private static final String PROP_BASEDIR = "com.beerandjesus.photogallery.basedir";

    public static Properties getProperties() {
        return PROPS;
    }

    /**
     * Default constructor using default app properties filename
     */
    public PhotoGalleryProperties() {
    }

    public PhotoGalleryProperties(String appPropertiesFile, String contextPropertiesFile) {
        this.appPropertiesFile = appPropertiesFile;
        this.contextPropertiesFile = contextPropertiesFile;
    }

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        String contextPath = servletContextEvent.getServletContext().getContextPath();
        LOG.info("Initializing context. contextPath=" + contextPath);

        // Load global app properties
        ClassPathResource appPropsResource = new ClassPathResource(appPropertiesFile);
        Properties appProps = new Properties();
        try {
            appProps.load(appPropsResource.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException("IOException loading application properties '" +
                    appPropertiesFile + "' from classpath", e);
        }

        // Get the basedir
        String baseDir = appProps.getProperty(PROP_BASEDIR);

        if (baseDir == null || baseDir.equals("")) {
            throw new RuntimeException("Property '" + PROP_BASEDIR + "' must be set in " +
                    "application properties file '" + appPropertiesFile + "'");
        }
        File contextPropsFile = new File(baseDir + contextPath, contextPropertiesFile);
        FileSystemResource contextPropsResource = new FileSystemResource(contextPropsFile);
        Properties contextProps = new Properties();
        try {
            contextProps.load(contextPropsResource.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException("IOException loading context properties from '" +
                    contextPropsFile.getAbsolutePath() + "' ", e);
        }

        PROPS.putAll(appProps);
        PROPS.putAll(contextProps);
        LOG.info("properties=" + PROPS.toString());

    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }

}
